<template>
  <basic-container>
    <el-container>
      <el-aside width="200px">
        <avue-tree :option="treeOption" :data="treeData" @node-click="nodeClick"></avue-tree>
      </el-aside>
      <el-main style="margin-left: 10px">
        <avue-crud
          :option="option"
          :table-loading="loading"
          :data="data"
          :page.sync="page"
          :permission="permissionList"
          v-model="form"
          @search-change="searchChange"
          @search-reset="searchReset"
          @selection-change="selectionChange"
          @current-change="currentChange"
          @size-change="sizeChange"
          @refresh-change="onLoad(page, query)"
          @on-load="onLoad"
        >
          <template #menuLeft v-if="isDev">
            <el-tag type="warning" effect="dark" size="medium"
              ><i class="el-icon-warning"></i> 部署的流程不显示？请查看使用文档或到 模型设计 中配置权限。
            </el-tag>
          </template>
          <template slot="menu" slot-scope="{ row }">
            <el-button
              v-if="permission.wf_process_start_flow"
              type="text"
              size="small"
              icon="el-icon-video-play"
              @click="dynamicRoute(row, 'start')"
              >发起</el-button
            >
          </template>
        </avue-crud>
      </el-main>
    </el-container>
  </basic-container>
</template>

<script>
import { processList as getList } from '@/api/plugin/workflow/process'
import { tree } from '@/api/plugin/workflow/category'

import { mapGetters } from 'vuex'

import exForm from '../mixins/ex-form'

export default {
  mixins: [exForm],
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        size: 'mini',
        height: 'auto',
        calcHeight: 30,
        tip: false,
        border: true,
        selection: true,
        dialogType: 'drawer',
        addBtn: false,
        editBtn: false,
        delBtn: false,
        align: 'center',
        searchMenuSpan: 6,
        searchSize: 'mini',
        column: [
          {
            label: '流程名称',
            prop: 'name',
            overHidden: true,
            search: true
          },
          {
            label: '流程标识',
            prop: 'key',
            overHidden: true,
            search: true
          },
          {
            label: '流程分类',
            row: true,
            type: 'tree',
            dicData: [],
            props: {
              label: 'name',
              value: 'id'
            },
            prop: 'category'
          },
          {
            label: '版本',
            prop: 'version',
            width: 90
          },
          {
            label: '状态',
            prop: 'status',
            dicData: [
              {
                label: '激活',
                value: 1
              },
              {
                label: '挂起',
                value: 2
              }
            ]
          }
        ]
      },
      data: [],
      row: '',
      categoryVisible: false,
      treeData: [],
      treeOption: {
        size: 'mini',
        addBtn: false,
        props: {
          label: 'name',
          value: 'id'
        }
      }
    }
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.deployment_add, false),
        viewBtn: this.vaildData(this.permission.deployment_view, false),
        delBtn: this.vaildData(this.permission.deployment_delete, false),
        editBtn: this.vaildData(this.permission.deployment_edit, false)
      }
    },
    ids() {
      let ids = []
      this.selectionList.forEach(ele => {
        ids.push(ele.id)
      })
      return ids.join(',')
    },
    isDev() {
      return process.env.NODE_ENV === 'development'
    }
  },
  mounted() {
    this.getCategoryList()
  },
  methods: {
    getCategoryList() {
      tree().then(res => {
        const data = res.data.data
        this.findObject(this.option.column, 'category').dicData = this.deepClone(data)

        this.treeData = data
        this.treeData.unshift({ id: '', name: '全部' })
      })
    },
    nodeClick({ id }) {
      this.categoryId = id
      this.searchChange(this.query)
    },
    searchReset() {
      this.query = {}
      this.onLoad(this.page)
    },
    searchChange(params, done) {
      this.query = params
      this.onLoad(this.page, params)
      if (done && typeof done == 'function') done()
    },
    selectionChange(list) {
      this.selectionList = list
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    onLoad(page, params = {}) {
      this.loading = true

      if (this.categoryId) params['category'] = this.categoryId
      else delete params['category']

      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data
        this.page.total = data.total
        this.data = data.records
        this.loading = false
      })
    }
  }
}
</script>

<style></style>
